﻿/***************************************************************
*       
* add by OceanHo 2015/9/2 11:17:07
*
****************************************************************/

using oceanho.webapi.factory.httpEntity;
using oceanho.webapi.factory.httpUtility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace oceanho.webapi.factory.httpCore.Binder
{
    /// <summary>
    /// 定义一个类，该类实现了IBinder接口，为Application的请求义务方法提供默认参数解析功能
    /// </summary>
    public class DefaultBinder : IBinder
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="parameterInfo"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public object GetModel(System.Reflection.ParameterInfo parameterInfo, System.Web.HttpContext context)
        {
            object obj = null;
            if (parameterInfo.ParameterType == typeof(UploadParameter))
            {
                obj = FileUploadUtility.BuildUploadParameter(context.Request, context.Server);
            }
            else if (parameterInfo.ParameterType == typeof(DownloadParameter))
            {
                obj = FileUploadUtility.BuildDownlodParameter(context.Request, context.Server);
            }

            try
            {
                obj = obj == null ? BinderUtility.ResolverAsObject(
                    parameterInfo.Name,
                    parameterInfo.DefaultValue, parameterInfo.ParameterType, context.Request) : obj;
            }
            catch
            {
                obj = parameterInfo.DefaultValue;
            }
            return obj;
        }
    }
}
